package it.canraid.leaflet.control.activity;

import it.canraid.control.IHandlerCommunication;
import it.canraid.leaflet.R;
import it.canraid.leaflet.control.ClickButtonListener;
import it.canraid.leaflet.control.DrawTextListener;
import it.canraid.leaflet.control.EraseRestoreListener;
import it.canraid.leaflet.control.Kernel;
import it.canraid.leaflet.control.TouchCutterListener;
import it.canraid.leaflet.model.LinePill;
import it.canraid.leaflet.model.ReadMessagePill;
import it.canraid.leaflet.model.StatusPill;
import it.canraid.leaflet.model.WordPill;
import it.canraid.leaflet.view.CreateMessageView;
import it.canraid.leaflet.view.FlingPanel;
import it.canraid.leaflet.view.ReadMessageView;
import it.canraid.leaflet.view.TogglePanel;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Toast;

public class DrawActivity extends Activity implements IHandlerCommunication {
    private ReadMessagePill rmp = null;
    public EditText text = null;
    public TogglePanel pocketPanel = null;
    public CreateMessageView textBody = null;
    public CreateMessageView drawBody = null;
    public ViewGroup top = null;
    public View bodyPanel = null;
    public View leftButton = null;
    public View rightButton = null;
    public View eraseButton = null;
    public View restoreButton = null;
    public View[] fontButton = new View[3];
    public View[] dimensionButton = new View[3];
    public View[] colorButton = new View[3];
    public View[] cutterButton = new View[6];

    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	Bundle asciiRMP = getIntent().getExtras();
	if (asciiRMP != null) {
	    String xml = asciiRMP.getString(ReadMessagePill.class.getName());
	    try {
		rmp = new ReadMessagePill(xml);
	    } catch (Exception e) {
		e.printStackTrace();
		Toast.makeText(this, "Errore interno: Impossibile deserializzare il messaggio.", Toast.LENGTH_LONG).show();
		rmp = new ReadMessagePill(new ArrayList<WordPill>(), new ArrayList<LinePill>(), new StatusPill(""));
	    }
	}
	setContentView(R.layout.foreground);
	LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	leftButton = findViewById(R.id.leftMenuImage);
	rightButton = findViewById(R.id.rightMenuImage);
	eraseButton = findViewById(R.id.eraseImage);
	restoreButton = findViewById(R.id.restoreImage);
	top = (ViewGroup) findViewById(R.id.topPanel);
	ViewGroup cutter = (ViewGroup) findViewById(R.id.cutterPanel);
	ViewGroup notepad = (ViewGroup) findViewById(R.id.textDrawPanel);
	notepad.setEnabled(false);

	if (rmp == null) {
	    Kernel.R().setText("Testo di prova");
	    View contentPocket = layoutInflater.inflate(R.layout.pocket, null);
	    pocketPanel = new TogglePanel(this, contentPocket);
	    pocketPanel.setId(Kernel.ID_TOGGLE_PANEL);
	    RelativeLayout.LayoutParams pocketPanelLP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	    pocketPanelLP.addRule(RelativeLayout.CENTER_HORIZONTAL, 1);
	    pocketPanelLP.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
	    top.addView(pocketPanel, pocketPanelLP);
	    pocketPanel.bringToFront();
	    drawBody = new CreateMessageView(this, Kernel.ID_DRAW_VIEW);
	    drawBody.setOnTouchListener(new DrawTextListener(this));
	    drawBody.setEnabled(true);
	    textBody = new CreateMessageView(this, Kernel.ID_TEXT_VIEW);
	    textBody.setOnTouchListener(new DrawTextListener(this));
	    textBody.setEnabled(true);
	    bodyPanel = new FlingPanel(this);
	    fontButton[0] = findViewById(R.id.font1Image);
	    fontButton[1] = findViewById(R.id.font2Image);
	    fontButton[2] = findViewById(R.id.font3Image);
	    dimensionButton[0] = findViewById(R.id.sImage);
	    dimensionButton[1] = findViewById(R.id.mImage);
	    dimensionButton[2] = findViewById(R.id.lImage);
	    colorButton[0] = findViewById(R.id.redImage);
	    colorButton[1] = findViewById(R.id.blackImage);
	    colorButton[2] = findViewById(R.id.blueImage);
	    text = (EditText) findViewById(R.id.textEdit);
	    text.setText(Kernel.R().getText());
	    cutterButton[0] = findViewById(R.id.cut0Image);
	    cutterButton[1] = findViewById(R.id.cut1Image);
	    cutterButton[2] = findViewById(R.id.cut2Image);
	    cutterButton[3] = findViewById(R.id.cut3Image);
	    cutterButton[4] = findViewById(R.id.cut4Image);
	    cutterButton[5] = findViewById(R.id.cut5Image);
	    Kernel.R().setFrequencyRefreshTouchDraw(Kernel.REFRESH_FREQUENCY_DRAW);
	    new ClickButtonListener(this, leftButton, Kernel.TEXT_IMAGE, 0);
	    new ClickButtonListener(this, rightButton, Kernel.TEXT_IMAGE, 0);
	    new ClickButtonListener(this, dimensionButton[0], Kernel.S_IMAGE, 1);
	    Kernel.R().sizeCurrentDraw = Kernel.SIZE_DRAW[0];
	    Kernel.R().sizeCurrentText = Kernel.SIZE_TEXT[0];
	    new ClickButtonListener(this, dimensionButton[1], Kernel.M_IMAGE, 0);
	    new ClickButtonListener(this, dimensionButton[2], Kernel.L_IMAGE, 0);
	    new ClickButtonListener(this, colorButton[0], Kernel.RED_IMAGE, 0);
	    new ClickButtonListener(this, colorButton[1], Kernel.BLACK_IMAGE, 1);
	    Kernel.R().colorCurrent = Color.BLACK;
	    new ClickButtonListener(this, colorButton[2], Kernel.BLUE_IMAGE, 0);
	    new ClickButtonListener(this, fontButton[0], Kernel.FONT_1_IMAGE, 0);
	    new ClickButtonListener(this, fontButton[1], Kernel.FONT_2_IMAGE, 0);
	    new ClickButtonListener(this, fontButton[2], Kernel.FONT_3_IMAGE, 1);
	    Kernel.R().fontCurrent = Kernel.FONT[2];
	    new EraseRestoreListener(this, eraseButton, R.drawable.pocket_ready, R.drawable.pocket_click);
	    new EraseRestoreListener(this, restoreButton, R.drawable.pocket_ready, R.drawable.pocket_click);
	    eraseButton.setEnabled(false);
	    eraseButton.setAlpha(Kernel.ALPHA_DISABLE_VIEW);
	    restoreButton.setEnabled(false);
	    restoreButton.setAlpha(Kernel.ALPHA_DISABLE_VIEW);
	    cutter.setEnabled(true);
	    cutter.setOnTouchListener(new TouchCutterListener(this));
	    text.addTextChangedListener(new TextWatcher() {
		public void afterTextChanged(Editable s) {
		    Kernel.R().setText(s.toString());
		}

		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}

		public void onTextChanged(CharSequence s, int start, int before, int count) {
		}

	    });
	} else {
	    cutter.setEnabled(false);
	    cutter.setAlpha(Kernel.ALPHA_DISABLE_VIEW);
	    eraseButton.setVisibility(View.INVISIBLE);
	    restoreButton.setVisibility(View.INVISIBLE);
	    leftButton.setVisibility(View.INVISIBLE);
	    rightButton.setVisibility(View.INVISIBLE);
	    ViewGroup contentPocket = new FrameLayout(this);// //////////////////////////
	    View dummy = new View(this);// ////////////////////////////////////////////////
	    FrameLayout.LayoutParams lpDummy = new FrameLayout.LayoutParams(200, 400);// ///
	    contentPocket.addView(dummy, lpDummy);// ////////////////////////////////////
	    contentPocket.setBackgroundColor(Color.YELLOW);// ///////////////////////////
	    pocketPanel = new TogglePanel(this, contentPocket);
	    pocketPanel.setId(Kernel.ID_TOGGLE_PANEL);
	    RelativeLayout.LayoutParams pocketPanelLP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	    pocketPanelLP.addRule(RelativeLayout.CENTER_HORIZONTAL, 1);
	    pocketPanelLP.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
	    notepad.addView(pocketPanel, pocketPanelLP);
	    bodyPanel = new ReadMessageView(this);
	    ReadMessageView readMessageView = (ReadMessageView) bodyPanel;
	    readMessageView.refresh(rmp.words, rmp.lines);
	}

	RelativeLayout.LayoutParams drawLParams_draw = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
	drawLParams_draw.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
	drawLParams_draw.addRule(RelativeLayout.BELOW, R.id.cutterPanel);
	notepad.addView(bodyPanel, drawLParams_draw);
	pocketPanel.bringToFront();
	ActionBar ab = getActionBar();
	ab.hide();
    }

    @Override
    public Handler getMyHandler() {
	// TODO Auto-generated method stub
	return null;
    }

    @Override
    public void addExternalHandler(Handler handler) {
	// TODO Auto-generated method stub

    }
}